<?php
namespace Home\Controller;
    use Home\Common\Common;
class IndexController extends Common {
    /**
     * 进入首页
     */
    public function index(){
        $this->display();
    }
    public function information(){
        $info=M('staffinfo')->where('stf_id='.$_SESSION['aid'])->find();
        $this->assign('info',$info);
        $this->display();
    }

    /**
     * 登录判断
     */
    public function login(){
        if(empty($_POST['submit'])){
            if($_SESSION['aid']){
                $this->success('已登录，正在为您跳转页面！',U('index'));
            }
            $this->display();
        }elseif($_COOKIE['aid']&&$_COOKIE['amd5']){//判断是否记住密码
            $admin=M('staffinfo');
            $info=$admin->where(array('stf_id'=>$_COOKIE['aid'],'md5'=>$_COOKIE['amd5']))->find();
            if($info['adb']>3){
                $this->error('您的账号已被冻结，请明天在试！',U('index'));
            }
            if($info){
                $md5name=md5($info['stf_name'].rand(10,1000)); //重新随机加密姓名
                $admin->where(array('stf_id'=>$info['stf_id']))->save(array('md5'=>$md5name));//存入加密字符串
                cookie('amd5',$md5name);//存入cookie  加密

                $admin->where(array('stf_id'=>$info['stf_id']))->save(array('daid'=>session_id()));
                $_SESSION['aid']=$_COOKIE['aid'];

                $this->success('登录成功！',U('index'));
            }else{//未记住密码
                cookie('aid',null);
                cookie('amd5',null);
                $this->error('警告！正在进行非法操作！',U('index/login'));
            }

        }else{
            $admin=M('staffinfo');
            $info=$admin->where(array('stf_name'=>$_POST['name'],'stf_pwd'=>md5($_POST['password'])))->find();
          
          	if(!empty($info['rd_time'])){
            	 $this->error('员工已离职，不可登录！',U('login'));
            }
          
            if($info['adb']>3){
                $this->error('您的账号已被冻结，请明天在试！',U('login'));
            }
            if($info){//登录成功

                $code=I('code');
                $verify=new\Think\Verify();
                if(!$verify->check($code,1)){
                   $this->error('验证码输入有误');
                }

                if(!empty($_POST['jz']))//记住密码
                {
                    $md5name=md5($info['stf_name'].rand(10,1000)); //姓名随机并加密
                    $admin->where(array('stf_id'=>$info['stf_id']))->save(array('md5'=>$md5name));//存入加密字符串
                    cookie('amd5',$md5name);//存入cookie  加密
                    cookie('aid',$info['stf_id']);//存入cookie  id
                }
                $admin->where(array('stf_id'=>$info['stf_id']))->save(array('daid'=>session_id()));
                session('aid',$info['stf_id']);//存入session id

                $this->success('登录成功！',U('index'));
            }
            else{
                $this->error('登录失败！',U('login'));
            }
        }
    }

    /**
     * 退出登录
     */
    public function out(){
        if($_SESSION['aid']){
            M('staffinfo')->where(array('stf_id'=>$_SESSION['aid']))->save(array('md5'=>'','daid'=>''));
        }
        unset($_SESSION['aid']);
        cookie('aid',null);
        cookie('amd5',null);
        $this->success('退出成功！',U('login'));
    }

    /**
     * 生成验证码
     */
    public function verify(){
        $Verify=new \Think\Verify();
        $Verify->length=4;
        $Verify->entry(1);
    }
}